package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class VideoServiceImpl implements VideoService {


    @Autowired
    private MongoTemplate mongoTemplate;

    //视频列表分页
    @Override
    public PageBeanVo findRecommendVideoByPage(Long userId, Integer pageNum, Integer pageSize) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        ).with(Sort.by(Sort.Order.desc("date"))).skip((pageNum - 1) * pageSize).limit(pageSize);

        List<RecommendVideo> recommendVideos = mongoTemplate.find(query, RecommendVideo.class);

        List<Video> videoList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(recommendVideos)) {
            for (RecommendVideo recommendVideo : recommendVideos) {
                Video video = mongoTemplate.findById(recommendVideo.getVideoId(), Video.class);

                videoList.add(video);
            }
        }

        return new PageBeanVo(pageNum, pageSize, 0L, videoList);

    }

    @Autowired
    private RedisIdService redisIdService;
    //发布视频
    @Override
    public void publishVideo(Video video) {
        Long vid = redisIdService.getNextId(ConstantUtil.VIDEO_ID);
        video.setVid(vid);
        mongoTemplate.save(video);
        RecommendVideo recommendVideo = new RecommendVideo();
        recommendVideo.setUserId(video.getUserId());
        recommendVideo.setDate(video.getCreated());
        recommendVideo.setVid(video.getVid());
        recommendVideo.setVideoId(video.getId());
        recommendVideo.setScore(RandomUtil.randomDouble(97, 99));
        mongoTemplate.save(recommendVideo);
    }


    //后台视频分页
    @Override
    public PageBeanVo findVideoByPage(Integer pageNum, Integer pageSize, String sortProp, String sortOrder, Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        if (StrUtil.equals(sortOrder, "desc")) {
            query = query.with(Sort.by(Sort.Order.desc(sortProp))).skip((pageNum - 1) * pageSize).limit(pageSize);
        } else {
            query = query.with(Sort.by(Sort.Order.asc(sortProp))).skip((pageNum - 1) * pageSize).limit(pageSize);
        }

        List<Video> videoList = mongoTemplate.find(query, Video.class);

        Query query1 = new Query(Criteria.where("userId").is(userId));

        long count = mongoTemplate.count(query1, Video.class);

        return new PageBeanVo(pageNum, pageSize, count, videoList);
    }

    //根据id寻找视频
    @Override
    public Video findById(ObjectId pulishId) {

        return  mongoTemplate.findById(pulishId, Video.class);
    }
}
